package org.fas.keymanager
{
	import flash.display.DisplayObjectContainer;
	import flash.events.KeyboardEvent;
	/**
	 * Key Manager
	 * 
	 */	
	public class KM
	{
		private static var items:KMitemArray = new KMitemArray(); 
		private static var keyLib:Object = new Object();
		public static function install(_container:DisplayObjectContainer):void{
			_container.addChild(new KMstage());
			loginKeyList(87,"w");
			loginKeyList(65,"a");
			loginKeyList(83,"s");
			loginKeyList(68,"d");
			loginKeyList(38,"up");
			loginKeyList(37,"left");
			loginKeyList(40,"down");
			loginKeyList(39,"right");
			loginKeyList(49,"1");
			loginKeyList(50,"2");
			loginKeyList(51,"3");
			loginKeyList(52,"4");
			loginKeyList(53,"5");
			loginKeyList(54,"6");
			loginKeyList(55,"7");
			loginKeyList(56,"8");
			loginKeyList(57,"9");
			loginKeyList(58,"0");
			loginKeyList(13,"enter");
			loginKeyList(32,"space");
			//
			loginKeyList(66,"b");
			loginKeyList(67,"c");
			loginKeyList(69,"e");
			loginKeyList(70,"f");
			loginKeyList(71,"g");
			loginKeyList(72,"h");
			loginKeyList(73,"i");
			loginKeyList(74,"j");
			loginKeyList(75,"k");
			loginKeyList(76,"l");
			loginKeyList(77,"m");
			loginKeyList(78,"n");
			loginKeyList(79,"o");
			loginKeyList(80,"p");
			loginKeyList(81,"q");
			loginKeyList(82,"r");
			loginKeyList(84,"t");
			loginKeyList(85,"u");
			loginKeyList(86,"v");
			loginKeyList(88,"x");
			loginKeyList(89,"y");
			loginKeyList(90,"z");
		}
		private static function loginKeyList(_code:int,_char:String):void{
			keyLib[_char] = _code;
		}
		public static function loginItemByCode(_keyCode:int,_item:KMitem):void{
			items.a(_keyCode,_item);
		}
		public static function loginItemByChar(_char:String,_item:KMitem):void{
			var _code:int = getCodeByChar(_char);
			if(_code!=0 && _code!=-1){
				loginItemByCode(_code,_item);
			}else{
				trace("Alarm:KM.loginItemByChar,error _char>>"+_char);
			}
		}
		public static function loginByCode(_keyCode:int,_ob:IKMListener,_arg:int):void{
			loginItemByCode(_keyCode,new KMitem(_ob.keyListener,_arg));
		}
		public static function loginByChar(_char:String,_ob:IKMListener,_arg:int):void{
			var _code:int = getCodeByChar(_char);
			if(_code!=0 && _code!=-1){
				loginByCode(_code,_ob,_arg);
			}else{
				trace("Alarm:KM.loginByChar,error _char>>"+_char);
			}
		}
		public static function getCodeByChar(_char:String):int{
			return keyLib[_char] as int;
		}
		public static function onKeyDown(_e:KeyboardEvent):void{
			//trace("keyDown",_e.charCode,_e.keyCode,_e.ctrlKey,_e.shiftKey,_e.altKey,_e.toString());
			if(items.c(_e.keyCode)!=null){
				items.c(_e.keyCode).func(items.c(_e.keyCode).arg);
			}else{
				trace("Alarm:KM.items.c(",_e.keyCode,") = null");
			}
		}
		public static function onKeyUp(_e:KeyboardEvent):void{
			//trace("keyUp",_e.charCode,_e.keyCode,_e.ctrlKey,_e.shiftKey,_e.altKey,_e.toString());
			if(items.c(_e.keyCode)!=null){
				items.c(_e.keyCode).func(items.c(_e.keyCode).arg,KeyboardEvent.KEY_UP);
			}else{
				trace("Alarm:KM.items.c(",_e.keyCode,") = null");
			}
		}
	}
}